﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sac.Web;

public partial class Admin_FanCodeToSysCodeManage : System.Web.UI.Page
{
    FanCodeToSysCodeOperate fan = new FanCodeToSysCodeOperate();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gridFanStateList.DataSource = fan.GetFanInfoListByPlantName(dropPlantName.Text);
            gridFanStateList.DataBind();
        }
    }

    protected void btnSure_Click(object sender, EventArgs e)
    {
        FanCodeToSysCode fanInfo = new FanCodeToSysCode();
        fanInfo.PlantName = dropPlantName.SelectedItem.Text;
        try
        {
            fanInfo.FanCode = Convert.ToInt32(textFanCode.Text.Trim());
        }
        catch
        {
            Response.Write("<script>alert('状态码必须为数字！')</script>");
            return;
        }
        fanInfo.FanDesc = textFanDesc.Text.Trim();
        fanInfo.SysCode = Convert.ToInt32(dropSysState.SelectedItem.Value);
        fanInfo.SysDesc = dropSysState.SelectedItem.Text;
        if (lblIdKey.Text == "")
        {
            if (fan.Insert(fanInfo))
            {
                textFanCode.Text = "";
                textFanDesc.Text = "";
            }
            else
            {
                Response.Write("<script>alert('添加失败，请检查风机状态码十分重复！')</script>");
            }
        }
        else
        {
            fanInfo.IdKey = Convert.ToInt32(lblIdKey.Text);
            if (fan.UpdateByIdKey(fanInfo))
            {
                textFanCode.Text = "";
                textFanDesc.Text = "";
            }
            else
            {
                Response.Write("<script>alert('更新失败！')</script>");
            }
        }
        gridFanStateList.DataSource = fan.GetFanInfoListByPlantName(dropPlantName.Text);
        gridFanStateList.DataBind();
    }

    protected void dropPlantName_SelectedIndexChanged(object sender, EventArgs e)
    {
        //if (lblIdKey.Text != "")
        //{
        //    lblIdKey.Text = "";
        //    textFanCode.Text = "";
        //    textFanDesc.Text = "";
        //}
        gridFanStateList.DataSource = fan.GetFanInfoListByPlantName(dropPlantName.Text);
        gridFanStateList.DataBind();
    }
    protected void gridFanStateList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string idKey = gridFanStateList.DataKeys[e.NewSelectedIndex].Value.ToString();
        FanCodeToSysCode fanInfo = new FanCodeToSysCode();
        fanInfo = fan.GetFanInfoByIdKey(idKey);
        lblIdKey.Text = fanInfo.IdKey.ToString();
        dropPlantName.SelectedValue = fanInfo.PlantName;
        textFanCode.Text = fanInfo.FanCode.ToString();
        textFanDesc.Text = fanInfo.FanDesc;
        dropSysState.SelectedValue = fanInfo.SysCode.ToString();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        lblIdKey.Text = "";
        dropPlantName.SelectedIndex = 0;
        textFanCode.Text = "";
        textFanDesc.Text = "";
        dropSysState.SelectedIndex = 0;
    }
    protected void gridFanStateList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string idKey = gridFanStateList.DataKeys[e.RowIndex].Value.ToString();
        if (fan.DeleteByIdKey(idKey))
        {
            gridFanStateList.DataSource = fan.GetFanInfoListByPlantName(dropPlantName.Text);
            gridFanStateList.DataBind();
        }
        else
        {
            Response.Write("<script>alert('删除失败！')</script>");
        }
    }
}
